package com.cskaoyan.wx.controller;


import com.cskaoyan.bean.OrderGoods;
import com.cskaoyan.bo.OrderIdBo;
import com.cskaoyan.bo.WxOrderCommentBo;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.wx.bean.bo.WxOrderSubmitBo;
import com.cskaoyan.wx.bean.vo.DetailVo;
import com.cskaoyan.wx.bean.vo.WxOrderSubmitVo;
import com.cskaoyan.wx.bean.vo.index.ListShowTypeVo;
import com.cskaoyan.wx.service.WxOrderService;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: libugu@cskaoyan.com
 * @date 2021/4/11 - 21:30
 */
@RestController
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;

    @RequestMapping("wx/order/goods")
    public BaseRespVo goods(Integer orderId, Integer goodsId) {

        OrderGoods orderGoods = wxOrderService.goods(orderId, goodsId);

        return BaseRespVo.ok(orderGoods);

    }


    @RequestMapping("wx/order/comment")
    public BaseRespVo comment(@RequestBody WxOrderCommentBo wxOrderCommentBo) {

        int i = wxOrderService.comment(wxOrderCommentBo);

        return BaseRespVo.ok();
    }

    @RequestMapping("wx/order/list")
    public BaseRespVo list(Integer showType, Integer page, Integer size) {

        ListShowTypeVo listShowTypeVo = wxOrderService.list(showType, page, size);
        return BaseRespVo.ok(listShowTypeVo);

    }

    @RequestMapping("wx/order/cancel")
    public BaseRespVo cancel(@RequestBody OrderIdBo orderIdBo) {

        Integer orderId = orderIdBo.getOrderId();
        wxOrderService.cancel(orderId);

        return BaseRespVo.ok();

    }

    @RequestMapping("wx/order/delete")
    public BaseRespVo delete(@RequestBody OrderIdBo orderIdBo) {

        Integer orderId = orderIdBo.getOrderId();
        wxOrderService.delete(orderId);
        return BaseRespVo.ok();

    }

    @RequestMapping("wx/order/detail")
    public BaseRespVo detail(Integer orderId) {

        DetailVo detailVo = wxOrderService.detail(orderId);

        return BaseRespVo.ok(detailVo);
    }

    @RequestMapping("wx/order/refund")
    public BaseRespVo refund(@RequestBody OrderIdBo orderIdBo) {

        Integer orderId = orderIdBo.getOrderId();
        wxOrderService.refund(orderId);
        return BaseRespVo.ok();
    }

    @RequestMapping("wx/order/confirm")
    public BaseRespVo confirm(@RequestBody OrderIdBo orderIdBo) {
        Integer orderId = orderIdBo.getOrderId();
        wxOrderService.confirm(orderId);
        return BaseRespVo.ok();
    }

    @RequestMapping("wx/order/submit")
    public BaseRespVo submit(@RequestBody WxOrderSubmitBo wxOrderSubmitBo) {

        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();

        WxOrderSubmitVo wxOrderSubmitVo = wxOrderService.submit(wxOrderSubmitBo, username);

        if (wxOrderSubmitVo == null) {
            return BaseRespVo.error("下单失败");
        }

        return BaseRespVo.ok(wxOrderSubmitVo);
    }

    @RequestMapping("wx/order/prepay")
    public BaseRespVo prepay(@RequestBody WxOrderSubmitVo wxOrderSubmitVo) {

        int i = wxOrderService.prepay(wxOrderSubmitVo.getOrderId());

        if(i==400){
            return BaseRespVo.error("订单异常");
        }else if(i==404) {
            return BaseRespVo.error("付款失败");
        }
        return BaseRespVo.ok(wxOrderSubmitVo);
    }


}
